package com.cqut.forum.dao.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ArticleReplyDao;

/**
 * 博客、心情、讨论区评论dao实现类
 * @author yangxun
 */
@Component
public class ArticleReplyDaoImp extends BaseDaoImp implements ArticleReplyDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getArticleReplys(int articleId, int index,
			int size) {
		String sql = "select u.user_photo,u.user_name,ar.article_reply_contents," +
				"ar.article_reply_time,ar.article_id,ar.article_reply_id from user u,article_reply ar,article a " +
				"where u.user_id=ar.user_id and ar.article_id=a.article_id and a.article_id=? and a.article_state=true " +
				"order by ar.article_reply_time";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(articleId);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, pages);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getArticleReplys(List<Integer> articleIds) {
		String sql = "select u.user_id,u.user_photo,u.user_name,ar.article_reply_contents," +
				"ar.article_reply_time,ar.article_id from user u,article_reply ar " +
				"where ar.user_id = u.user_id and ar.article_id in (";
		
		List<Object> parameters = new ArrayList<Object>();
		for(Integer i : articleIds){
			sql = sql + "?,";
			parameters.add(i);
		}
		sql = sql.substring(0,sql.length()-1)+") order by ar.article_reply_time";
		//调用父类中的方法获取数据
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);

		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getByOwnerId(int userId, int index,int size) {
		String sql = "select u.user_photo,u.user_name,ar.article_reply_contents," +
				"ar.article_reply_time,ar.article_id from user u,article_reply ar,article a " +
				"where u.user_id=a.owner_id and a.article_state=true and ar.article_id=a.article_id and u.user_id=? " +
				"order by ar.article_reply_time desc";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);

		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, pages);
		return result;
	}
}
